/*
 License
 
 Copyright © 2012 Kevin Kimmich
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to 
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 the Software, and to permit persons to whom the Software is furnished to do so, 
 subject to the following conditions:
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 @file
 */
#ifndef _kuato_http_h
#define _kuato_http_h

#ifdef __cplusplus
extern "C" {
#endif
	
#include "kuato_types.h"
	
	
	extern const char CRLF[];
	extern const char CRLFCRLF[];
	
	extern const result_code HTTP_OK;
	extern const result_code HTTP_BAD_REQUEST;
	extern const result_code HTTP_UNAUTHORIZED;
	extern const result_code HTTP_NOT_FOUND;
	extern const result_code HTTP_METHOD_NOT_ALLOWED;
	extern const result_code HTTP_INTERNAL_SERVER_ERROR;
	
	/**
	 Initialize the library.
	 */
	void www_init(void);
		
	/**
	 Open an HTTP server bound to a socket at address.
	 @param h The http server object.
	 @param address The address to listen to.
	 @param max_conn The maximum number of connections to queue.
	 @param cert_file The certificate file for an SSL socket.
	 @param key_file The key file for an SSL socket.
	 * 
	 * returns NULL on failure.
	 */
	connection_context* open_http_server(http_server_object* h,
										 struct sockaddr_in address, 
										 int max_conn, 
										 const char* cert_file, const char* key_file);
	
	/**
	 Close the http server context.
	 @param ctx The server context.
	 */
	void close_http_server(connection_context* ctx);
	
	/**
	 The chunked handler function.
	 @param rule The rule that was matched to call the handler.
	 @param m The method object for the request.
	 */
	void chunked_handler_func(http_match_rule_t* rule, method* m);
	
	/**
	 A helper function to do an http response.
	 @param m The response method.
	 @param mime_type The mime type of the response, e.g. text/html
	 @param http_rest The result code, e.g. HTTP_OK
	 @param headers Any custom headers.
	 @param body The response body (or NULL).
	 */
	void do_http_response(method* m, const char* mime_type, result_code http_result, stream* headers, stream* body);
	
	/**
	 Set the http server private data
	 @param h The http_server_object
	 @param priv The private data
	 */
	void http_server_object_set_private( http_server_object* h, void* priv );
	
	/**
	 Get the http private data
	 @param h The http_server_object
	 * 
	 * returns the private data.
	 */
	void* http_server_object_get_private( http_server_object* h );
	
	/**
	 Set the domain object private data.
	 @param d the domain_object.
	 @param priv the private data.
	 */
	void domain_object_set_private( domain_object* d, void* priv );
	
	/**
	 Get the domain object private data.
	 @param d The domain object
	 * 
	 * returns the domain object private data.
	 */
	void* domain_object_get_private( domain_object* d );

	/**
	 A generic handler for rejecting requests to use with match rules.
	 @param m The method object.
	 */
	void http_reject( method* m );
	
	/*!
	 Add a match rule to the domain.
	 @param d the domain to add the rule to.
	 @param htype The handler type, chunked or discrete.
	 @param handle_request The request handler. http_reject is pre-defined.
	 @param count the number of matches to test, 0,1,2,...,MAX_COMPARISONS are valid values. 0 is only valid for certain match types.
	 @param match_type,value,combination,etc...
	 * 
	 * The pattern for defining the rules is:
	 * match_type_t e.g. match_address,
	 * Match expression,
	 * ...Then if there are additional matches, specify the match combination.
	 * 
	 */
	void http_server_add_match_rule(domain_object* d, 
									handler_t htype,
									void(*handle_request)(method*),int count, ...);
	
	
#ifdef __cplusplus
}
#endif

#endif
